package com.yjd.api.home;

import com.yjd.api.util.PicsResourceUtils;
import com.yjd.api.util.TagUtils;
import com.yjd.comm.base.control.MobileBaseControl;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.base.model.PagerModel;
import com.yjd.comm.doctor.model.DoctorInfoDto;
import com.yjd.comm.doctor.service.IDoctorService;
import com.yjd.comm.org.model.OrgInfo;
import com.yjd.comm.org.service.IOrganizationService;
import com.yjd.comm.project.model.ProjectShowInfo;
import com.yjd.comm.project.service.IProjectService;
import com.yjd.comm.resource.model.ResourceModel;
import com.yjd.comm.resource.service.IResourceService;
import com.yjd.comm.tag.model.TagInfoDto;
import com.yjd.comm.tag.model.TagModel;
import com.yjd.comm.tag.service.ITagService;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.ServletUtil;
import com.yjd.comm.util.TypeShowUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by yjd-wap on 2017/7/3.
 */
@Controller
@Scope("prototype")
@RequestMapping("/hot/")
public class HotProjectControl extends MobileBaseControl {

    @Resource
    private ITagService tagService;

    @Resource
    private IDoctorService doctorService;

    @Resource
    private IOrganizationService organizationService;

    @Resource
    private IProjectService projectService;

    @Resource
    private IResourceService resourceService;

    /**
     * 热搜标签
     * @throws Exception
     */
    @RequestMapping("tag")
    public void tag() throws Exception {
        //返回热搜标签
      List<TagModel> list = tagService.getModelList(FrameUtil.newHashMap("top",1,"enable",1,"sort"," tag_sort desc "), DbWREnums.READ);
        successData(FrameUtil.newHashMap("hotTag",list));
    }

    /**
     * 搜索项目
     * @throws Exception
     */
    @RequestMapping("prj")
    public void prj() throws Exception {
        Map<String, Object> paramMap = ServletUtil.getPageParameterMap(request);
        paramMap.put("rows", "10");
        paramMap.put("is_used", "1");//下架项目不显示
        PagerModel pagerModel = projectService.getProjectListByKeys(paramMap);
        List<ProjectShowInfo> prjList = pagerModel.getDataList();
        List<Long> pjIdList = FrameUtil.getBeanFieldValues(prjList, "project_id");
        List<ResourceModel>  resourceModelList=null;
        if(pjIdList !=null){
            resourceModelList = resourceService.getModelList(FrameUtil.newHashMap("ref_type",2,"idList",pjIdList),DbWREnums.READ);
        }

        for (ProjectShowInfo showInfo:prjList){
            showInfo.setPicsUrl(ServletUtil.getFileUrl(PicsResourceUtils.getPicsList(resourceModelList,showInfo.getProject_id())));//设置项目的图片
        }
        this.successData(FrameUtil.newHashMap("data",prjList));
    }

    /**
     * 搜索机构
     * @throws Exception
     */
    @RequestMapping("org")
    public void org() throws Exception {
        Map<String, Object> paramMap = ServletUtil.getPageParameterMap(request);
        paramMap.put("rows", "10");
        PagerModel pagerModel = organizationService.getOrgListByKeys(paramMap);
        List<OrgInfo> orgList = pagerModel.getDataList();
        List<Long> orgIdList = FrameUtil.getBeanFieldValues(orgList, "org_id");
        List<ResourceModel>  resourceModelList=null;
        List<TagInfoDto> allTagList = null;
        if(orgIdList !=null){
            resourceModelList = resourceService.getModelList(FrameUtil.newHashMap("ref_type",1,"idList",orgIdList),DbWREnums.READ);
            allTagList = tagService.getTagNameList(FrameUtil.newHashMap("ref_type",1,"idList",orgIdList));
        }
        Map<Integer,String> showTypes =  TypeShowUtils.getDoctorTitle();

        for (OrgInfo orgInfo:orgList){
            orgInfo.setOrg_type(showTypes.get(orgInfo.getType()));//设置机构类型
            orgInfo.setPicsUrl(ServletUtil.getFileUrl(PicsResourceUtils.getPicsList(resourceModelList,orgInfo.getOrg_id())));//设置机构的图片
            orgInfo.setTagList(TagUtils.getTagNameList(allTagList,orgInfo.getOrg_id()));//设置机构标签
        }
        this.successData(FrameUtil.newHashMap("data",orgList));

    }

    /**
     * 搜索医生
     * @throws Exception
     */
    @RequestMapping("doctor")
    public void doctor() throws Exception {
        Map<String, Object> paramMap = ServletUtil.getPageParameterMap(request);
        paramMap.put("rows", "10");
        PagerModel pagerModel = doctorService.getDoctorListByKeys(paramMap);
        List<DoctorInfoDto> dcList = pagerModel.getDataList();
        List<Long> dcIdList = FrameUtil.getBeanFieldValues(dcList, "doctor_id");
        List<ResourceModel>  resourceModelList=null;
        List<TagInfoDto> allTagList = null;
        if(dcIdList !=null){
            resourceModelList = resourceService.getModelList(FrameUtil.newHashMap("ref_type",3,"idList",dcIdList),DbWREnums.READ);
            allTagList = tagService.getTagNameList(FrameUtil.newHashMap("ref_type",3,"idList",dcIdList));
        }
        Map<Integer,String> doctorTitles = TypeShowUtils.getDoctorTitle();
        for (DoctorInfoDto doctorInfoDto:dcList){
            doctorInfoDto.setPosition(doctorTitles.get(doctorInfoDto.getType()));//设置医生的级别
            doctorInfoDto.setPicsUrl(ServletUtil.getFileUrl(PicsResourceUtils.getPicsList(resourceModelList,doctorInfoDto.getDoctor_id())));//设置机构的图片
            doctorInfoDto.setTagList(TagUtils.getTagNameList(allTagList,doctorInfoDto.getDoctor_id()));//设置机构标签
        }
        this.successData(FrameUtil.newHashMap("data",dcList));
    }



}
